// A program to analyze data pulled from data.seattle.gov and provide information // about crimes in various neighborhoods of Seattle. // // This program demonstrates how meaningful, real problems can be solved on real // data using the programming skills learned in CSE142. There are no concepts // used in this program that was not taught in CSE142, though there are a few // unfamiliar library methods called (namely, split() and binarySearch()). import java.util.*; import java.io.*; public class CrimeData { public static final String[] CRIMES = {"AGGRAVATED ASSAULT", "AGGRAVATED ASSAULT-DV", "ARSON", "BURGLARY-COMMERCIAL", "BURGLARY-COMMERCIAL-SECURE PARKING", "BURGLARY-RESIDENTIAL", "BURGLARY-RESIDENTIAL-SECURE PARKING", "CAR PROWL", "DISORDERLY CONDUCT", "DUI", "FAMILY OFFENSE-NONVIOLENT", "GAMBLE", "HOMICIDE", "LIQUOR LAW VIOLATION", "LOITERING", "MOTOR VEHICLE THEFT", "NARCOTIC", "PORNOGRAPHY", "PROSTITUTION", "RAPE", "ROBBERY-COMMERCIAL", "ROBBERY-RESIDENTIAL", "ROBBERY-STREET", "SEX OFFENSE-OTHER", "THEFT-ALL OTHER", "THEFT-BICYCLE", "THEFT-BUILDING", "THEFT-SHOPLIFT", "TRESPASS", "WEAPON"}; public static final String[] NEIGHBORHOODS = {"ALASKA JUNCTION", "ALKI", "BALLARD NORTH", "BALLARD SOUTH", "BELLTOWN", "BITTERLAKE", "BRIGHTON/DUNLAP", "CAPITOL HILL", "CENTRAL AREA/SQUIRE PARK", "CHINATOWN/INTERNATIONAL DISTRICT", "CLAREMONT/RAINIER VISTA", "COLUMBIA CITY", "COMMERCIAL DUWAMISH", "COMMERCIAL HARBOR ISLAND", "DOWNTOWN COMMERCIAL", "EASTLAKE - EAST", "EASTLAKE - WEST", "FAUNTLEROY SW", "FIRST HILL", "FREMONT", "GENESEE", "GEORGETOWN", "GREENWOOD", "HIGH POINT", "HIGHLAND PARK", "HILLMAN CITY", "JUDKINS PARK/NORTH BEACON HILL", "LAKECITY", "LAKEWOOD/SEWARD PARK", "MADISON PARK", "MADRONA/LESCHI", "MAGNOLIA", "MID BEACON HILL", "MILLER PARK", "MONTLAKE/PORTAGE BAY", "MORGAN", "MOUNT BAKER", "NEW HOLLY", "NORTH ADMIRAL", "NORTH BEACON HILL", "NORTH DELRIDGE", "NORTHGATE", "PHINNEY RIDGE", "PIGEON POINT", "PIONEER SQUARE", "QUEEN ANNE", "RAINIER BEACH", "RAINIER VIEW", "ROOSEVELT/RAVENNA", "ROXHILL/WESTWOOD/ARBOR HEIGHTS", "SANDPOINT", "SLU/CASCADE", "SODO", "SOUTH BEACON HILL", "SOUTH DELRIDGE", "SOUTH PARK", "UNIVERSITY", "UNKNOWN", "WALLINGFORD"}; public static void main(String[] args) throws FileNotFoundException { Scanner data = new Scanner(new File("Crime_Data.csv")); Scanner console = new Scanner(System.in); // Uncomment this code to read the list of crimes and/or neighborhoods directly from the // file instead of using the preprocessed lists above. // System.out.println(getValues(data, "c")); // data.close(); // data = new Scanner(new File("Crime_Data.csv")); // System.out.println(getValues(data, "n")); int neighborhood; do { System.out.println("Choose a neighborhood: "); for (int i = 0; i < NEIGHBORHOODS.length; i++) { System.out.println(" " + (i + 1) + ": " + NEIGHBORHOODS[i]); } neighborhood = console.nextInt(); } while (neighborhood < 0 || neighborhood >= NEIGHBORHOODS.length); int[] counts = processData(data, neighborhood); printResults(counts); } // Reads the data in the input scanner and computes how many of each // type of crime occured in the specified neighborhood. Returns // an array where each element is the count of the type of crime // with the corresponding index in the list of crimes above. // // Scanner data - the input file to read from // int neighborhood - the number of the neighborhood chosen by the user public static int[] processData(Scanner data, int neighborhood) { int[] results = new int[CRIMES.length]; data.nextLine(); // throw out line that contains only field names while (data.hasNextLine()) { String line = data.nextLine(); String[] fields = line.split(","); String crime = fields[5]; String currNeighborhood = fields[10]; int neighborhoodIndex = Arrays.binarySearch(NEIGHBORHOODS, currNeighborhood); if (neighborhoodIndex == (neighborhood - 1)) { // subract one because the menu starts at 1 int index = Arrays.binarySearch(CRIMES, crime); if (index > 0) { results[index]++; } } } return results; } // Prints out the number of crimes of each type in the given array. // // int[] counts - the count of each type of crime. public static void printResults(int[] counts) { for (int i = 0; i < CRIMES.length; i++) { System.out.println(CRIMES[i] + ": " + counts[i]); } } // Reads the input file and produces a list of all crime or neighborhood // names found in the file, which is then returned. This list can be used // instead of the preprocessed list in case new crimes or neighborhoods // were added to the file. // // Scanner data - the input file to read from // String category - either "c" to get a list of crimes or "n" to get // a list of neighborhoods public static ArrayList getValues(Scanner data, String category) { ArrayList results = new ArrayList(); data.nextLine(); while (data.hasNextLine()) { String line = data.nextLine(); String[] fields = line.split(","); String val; if (category.equalsIgnoreCase("c")) { val = fields[5]; } else { val = fields[10]; } if (!results.contains(val)) { results.add(val); } } Collections.sort(results); return results; } }